package org.nokatag.tagjava;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
/**
 * 利用Flash实现在网页上建socket
 * @author wife
 *
 */
public class NkSocket  extends NokaTag{
	private String id=null;
	private boolean isbug=false;
	private String onDataFunction;
    private String openFunction=null;
    private String sendFunction=null;
    private String closeFunction=null;
    private String onConnection=null;
    private String ip=null;//ip
    private String port = null;//port
    private String sendmsg = null;//自动发送消息
    private String interval=null;//自动发送间隔
    private String autoConnection = "true";//加载自动连接
	
	private static final long serialVersionUID = 5234644088793337655L;
	public int doStartTag()throws JspException{
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		StringBuilder bodystr = new StringBuilder();
		JspWriter jspOut = pageContext.getOut();
		String basePath = basepath(request);
		bodystr.append(potjs(request));//加载Prototyp
		bodystr.append(SocketJS(request));//加载socketjs
		bodystr.append("<div id=\""+getId()+"\" style=\"width: 0px;height: 0px\"></div>");
		bodystr.append("<script type=\"text/javascript\">");
		bodystr.append("Event.observe(document, 'dom:loaded', function() {");
		bodystr.append("var nsocket"+getId()+" = {");
		bodystr.append("id:'"+getId()+"',");
		bodystr.append("rurl:'"+basePath+"',");
		if(null!=sendmsg && null!=interval){//需要自动发送
			bodystr.append("sendmsg:'"+sendmsg+"',");
			bodystr.append("interval:'"+interval+"',");
		}
		if(null!=ip){
			bodystr.append("ip:'"+getIp()+"',");
		}
		if(null!=port){
			bodystr.append("port:"+getPort()+",");
		}
		bodystr.append("isbug:"+isbug+",");
		bodystr.append("OnDataFunction:'"+(onDataFunction==null?"noka_"+getId()+"_onData":onDataFunction)+"',");
		bodystr.append("OnConnection:'"+(onConnection==null?"noka_"+getId()+"_onCon":onConnection)+"'");
		bodystr.append("};");
		bodystr.append(" var nokasocket"+getId()+" = new nksocket(nsocket"+getId()+");");
		bodystr.append(" nokasocket"+getId()+".show();");
		if(null!=ip && null!=port){
			bodystr.append(" nokasocket"+getId()+".setConfigf('"+ip+"',"+port+");");
			if("true".equals(autoConnection)){//自动连接
				bodystr.append(" nokasocket"+getId()+".OpenConnection('"+ip+"',"+port+");");
			}
		}
		bodystr.append("});");
		//-------------------------自定义方法---------------------------------
		if(null==onDataFunction){//数据到达方法为空
			bodystr.append("function noka_"+getId()+"_onData(a){}");
		}
		if(null==onConnection){//在连接时
			bodystr.append("function noka_"+getId()+"_onCon(a){}");
		}
		if(null!=openFunction){//打开连接
			bodystr.append("function "+openFunction+"(ip,port){$('"+id+"').open(ip,port);}");
		}
		if(null!=sendFunction){//发送数据
			bodystr.append("function "+sendFunction+"(msg){$('"+id+"').send(msg);}");
		}
		if(null!=closeFunction){//关闭连接
			bodystr.append("function "+closeFunction+"(){$('"+id+"').close();}");
		}
		 //-------------结束-------------------------------------------------
		bodystr.append(" </script>\n");
		try{
			jspOut.write(bodystr.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public boolean isIsbug() {
		return isbug;
	}
	public void setIsbug(boolean isbug) {
		this.isbug = isbug;
	}
	public String getOnDataFunction() {
		return onDataFunction;
	}
	public void setOnDataFunction(String onDataFunction) {
		this.onDataFunction = onDataFunction;
	}


	public String getCloseFunction() {
		return closeFunction;
	}


	public void setCloseFunction(String closeFunction) {
		this.closeFunction = closeFunction;
	}


	public String getOpenFunction() {
		return openFunction;
	}


	public void setOpenFunction(String openFunction) {
		this.openFunction = openFunction;
	}


	public String getSendFunction() {
		return sendFunction;
	}


	public void setSendFunction(String sendFunction) {
		this.sendFunction = sendFunction;
	}


	public String getOnConnection() {
		return onConnection;
	}


	public void setOnConnection(String onConnection) {
		this.onConnection = onConnection;
	}


	public String getIp() {
		return ip;
	}


	public void setIp(String ip) {
		this.ip = ip;
	}


	public String getPort() {
		return port;
	}


	public void setPort(String port) {
		this.port = port;
	}

	public String getSendmsg() {
		return sendmsg;
	}

	public void setSendmsg(String sendmsg) {
		this.sendmsg = sendmsg;
	}

	public String getInterval() {
		return interval;
	}

	public void setInterval(String interval) {
		this.interval = interval;
	}

	public String getAutoConnection() {
		return autoConnection;
	}

	public void setAutoConnection(String autoConnection) {
		this.autoConnection = autoConnection;
	}

}
